#!/usr/bin/ruby

require 'rubygems'
require 'pdf/writer'
require 'iconv'

#------------konstanty
_USERNAME = "klikleos"
_UHEL = 45
_VELIKOSTPISMA = 80
_BARVAPISMA = "c0c0c0"


pdf = PDF::Writer.new :paper => "A4", :orientation => :portrait
pdf.select_font("Helvetica")

_delkaTextu = pdf.text_line_width(_USERNAME, _VELIKOSTPISMA)
_vyskaTextu = pdf.font_height(_VELIKOSTPISMA)
_textXpozice = (pdf.page_width/2) - (_delkaTextu/2)
_textYpozice = (pdf.page_height/2) - (_vyskaTextu/2)

#-----------posun stredu po rotaci
_uhloprickaPul = Math.sqrt(_delkaTextu * _delkaTextu + _vyskaTextu * _vyskaTextu) / 2

_vertikalniPosun = Math.sin(_UHEL) * _uhloprickaPul - (_vyskaTextu / 2)
_finalYpozice = _textYpozice - _vertikalniPosun

_horizontalniPosun = (_delkaTextu / 2) - Math.cos(_UHEL) * _uhloprickaPul
_finalXpozice = _textXpozice + _horizontalniPosun


pdf.fill_color Color::RGB.from_html(_BARVAPISMA)
pdf.add_text(_finalXpozice, _finalYpozice, _USERNAME, _VELIKOSTPISMA, _UHEL)
  
datum = Time.new
datumFormatted = datum.strftime("%d.%m.%Y %H:%M:%S")

hfText = "Staženo uživatelm  z , " + datumFormatted

str_conv = Iconv.new('windows-1252', 'utf-8')
hfText = str_conv.iconv(hfText)

pdf.add_text_wrap(0, 10, pdf.page_width, hfText, 10, :center)
pdf.add_text_wrap(0, pdf.page_height - pdf.font_height(11) - 10, pdf.page_width, hfText, 11, :center)

pdf.save_as("test.pdf")

#system("pdftk skripta.pdf background test.pdf output pokus.pdf")
